<?php
/**
 * Created by PhpStorm.
 * User: qilin
 * Date: 2018/1/31
 * Time: 14:50
 */

namespace App\ServiceImpl;


use App\Model\Integral;
use App\Model\Vip;
use App\Service\VipService;

class VipServiceImpl implements VipService
{
    public function getVipList()
    {
        $data = Vip::where('vip_delete',0)->paginate(15);
        $integralData = Integral::all();
        foreach ($data as $key => $value){
            $data[$key]->vip_integral = $this->getUserIntegral($data[$key]->vip_consume);
        }
        $data->count = $data->toArray()['total'];
        return $data;
        // TODO: Implement getVipList() method.
    }
    public function getUserIntegral($userConsume){
        $integralData = Integral::orderBy('integral_num','asc')->get()->toArray();
        for($i=0;$i<count($integralData);$i++){
            if($userConsume<=$integralData[$i]['integral_num']){
                if($i==0){
                    return '普通会员';
                }
                return $integralData[$i]['integral_name'];
            }
        }
    }
    public function getVipInfo($id)
    {
        $data = Vip::where("vip_id",$id)->first();
        return $data;
        // TODO: Implement getVipInfo() method.
    }

    public function addVip($vip)
    {
        $vipData = new Vip();
        $vipData->vip_name = $vip['username'];
        $vipData->vip_tele = $vip['mobile'];
        $vipData->vip_sex = $vip['sex'];
        $vipData->vip_email = $vip['email'];
        $vipData->vip_image = $vip['image'];
        $vipData->vip_city = $vip['city'];
        $vipData->vip_notice = $vip['notice'];
//        $vipData->vip_integral = 1;
        $vipData->save();
//        // TODO: Implement addVip() method.
    }

    public function updateVip($vip)
    {
        Vip::where('vip_id',$vip['vip_id'])->update($vip);
        // TODO: Implement updateVip() method.
    }

    public function deleteVip($id)
    {
        Vip::where('vip_id',$id)->update(['vip_delete'=>1]);
        // TODO: Implement deleteVip() method.
    }

    public function getDeleteVip()
    {
        $data = Vip::where('vip_delete',1)->paginate(15);
        $data->count = $data->toArray()['total'];
        return $data;
        // TODO: Implement getDeleteVip() method.
    }

    public function recoveryVip($id)
    {
        Vip::where('vip_id',$id)->update(['vip_delete'=>0]);
        // TODO: Implement recoveryVip() method.
    }

    public function addClass($class)
    {
        $integral = new Integral();
        $integral->integral_name =$class['integral_name'];
        $integral->integral_num =$class['integral_num'];
        $integral->integral_discount =$class['integral_discount'];
        $integral->save();
        // TODO: Implement addClass() method.
    }

    public function deleteClass($id)
    {
        Integral::destroy($id);
        // TODO: Implement deleteClass() method.
    }
    public function getClassList()
    {
        $data = Integral::all();
        return $data;
        // TODO: Implement getClassList() method.
    }

    public function updateClass($class)
    {
        Integral::where('integral_id',$class['integral_id'])->update($class);
        // TODO: Implement updateClass() method.
    }

    public function getClass($id)
    {
        $data = Integral::find($id);
        return $data;
        // TODO: Implement getClass() method.
    }

}